package com.liang.leetcode.user.config;

import com.liang.leetcode.user.mapper.UserOAuthMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 布隆过滤器配置类
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class RBloomFilterConfigurer {

    private final UserOAuthMapper userOAuthMapper;

    /**
     * UserOAuth布隆过滤器 - 用于快速判断第三方账号是否已绑定
     * key格式: provider:providerUserId
     * 例如: github:123456, google:abc123
     */
    @Bean
    public RBloomFilter<String> userOAuthBloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("user_oauth_bloom_filter");
        // 预计10万条数据，误判率0.001
        bloomFilter.tryInit(100000L, 0.001);
        return bloomFilter;
    }
}